package com.bjsxt.userscenter.common.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.util.DateFormatUtil;
import com.bjsxt.userscenter.common.util.EncryptUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.common.util.VerifyCodeUtils;

public class BaseController
{
	@Resource
	protected DateFormatUtil dateFormatUtil;
	@Resource
	protected EncryptUtil encryptUtil;

	/*
	 * 定义一个String类的info对象 主要用来提示信息，String本身实现了序列化，同时String本身就在Spring容器中，他可以识别
	 */
	protected String info;

	/**
	 * 生成分页对象 因为不确定哪个controller还需要借用分页对象，所以需要把PageInfoUtil写到父类中
	 * 
	 * @param currentPage
	 * @param pageSize
	 */
	protected PageInfoUtil proccedPageInfo(String currentPage, String pageSize)
	{
		/* 分页 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		try
		{
			/*
			 * 将字符串转换成整数,有风险, 字符串为a,转换不成不整数
			 */
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfoUtil;
	}

	/*
	 * 验证码可能被多个controller调用，把验证码写到Basecontroller中
	 * HttpServletRequest需要有servlet包，放在common中，要求common也就有相应的包
	 * requestMapping需要webmvc的包 request需要servlet的包
	 * 注意此时的返回值是一个图片，直接放在src中可以在该位置生成一个验证码
	 */
	@RequestMapping("/randImg")
	public void randImg(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");

		// 生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		// 存入会话session
		HttpSession session = request.getSession(true);
		session.setAttribute("rand", verifyCode);
		// 生成图片
		int w = 200, h = 80;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
	}

	/* 拼装jui要求返回的结果 */
	protected JSONObject printJuiJson(HttpServletRequest request,String info)
	{
//		注意获取参数值的时候要加上双引号
		String statusCode = request.getParameter("statusCode");
		String navTabId = request.getParameter("navTabId");
		String rel = request.getParameter("rel");
		String callbackType = request.getParameter("callbackType");
		String forwardUrl = request.getParameter("forwardUrl");
		String confirmMsg = request.getParameter("confirmMsg");

		if (navTabId == null)
		{
			navTabId = "";
		}
		if (rel == null)
		{
			rel = "";
		}
		if (callbackType == null)
		{
			callbackType = "";
		}
		if (forwardUrl == null)
		{
			forwardUrl = "";
		}
		/*之所以这样写是因为在给定的模板中，他的返回值里面没有null，都是"，
		 * 同时resultJSON里面的只有info有信息		 */
		JSONObject resultJSON = new JSONObject();
		resultJSON.put("statusCode", "200");
		resultJSON.put("message", info);
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("confirmMsg", confirmMsg);
		return resultJSON;
	}

}
